package signalprocessing;



import ikelib.DelayThread;
import ikelib.DelayThreadInterface;
import ikelib.PlotColor;
import ikelib.wintime;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.border.EmptyBorder;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
/**
 * 
 */
import javax.swing.JPopupMenu;
import javax.swing.JButton;

import wcg.winDataBase;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JMenuItem;
import javax.swing.JMenu;



/**
 * @author Toshiharu Ikeda <ikeda.104@gmail.com>
 * 
 * 複数のグラフペインと時間軸を収納するクラス
 * グラフペインの動的な変更ができる。
 * 
 */
public class PlotPaneCollection extends JPanel implements DelayThreadInterface {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	public LinkedList<Integer> getAccViewCh() {
		return AccViewCh;
	}


	public HashMap<Integer, PlotTable> getPlot() {
		return Plot;
	}


	public PlotColor getCol() {
		return Col;
	}



	public void setParent(PlotPanelCollectionInterface parent) {
		Parent = parent;
	}
	public void setUp(PlotPanelCollectionInterface parent, LinkedList<Integer> accViewCh,HashMap<Integer, PlotTable> plot,PlotColor col) {
		Parent = parent;
		AccViewCh = accViewCh;
		Plot = plot;
		Col = col;
	}

	private LinkedList<Integer> AccViewCh = new LinkedList<Integer>();
	private HashMap<Integer,PlotTable> Plot = new HashMap<Integer,PlotTable>();
	private PlotColor Col = null;
	private PlotPanelCollectionInterface Parent=null;




	private PlotAxisTView Panel_TAxis=null;
	private PlotAxisXView Panel_XAxis=null;
	
	private PlotPanel Panel_Channels=null;


	private final ButtonGroup buttonGroup = new ButtonGroup();
	private final ButtonGroup buttonGroup2 = new ButtonGroup();
	
	private JRadioButtonMenuItem rdbtnmntmBwBlack;
	private JRadioButtonMenuItem rdbtnmntmColorWhite;
	private JRadioButtonMenuItem rdbtnmntmColorBlack;
	private JRadioButtonMenuItem rdbtnmntmBwWhite;
	private PlotPanel BasePanel;

	private PlotPanelCollectionConfiguration Conf=null;
	private JRadioButtonMenuItem rdbtnLegendTopLeft;
	private JMenu mnLegend;
	private JRadioButtonMenuItem rdbtnLegendTopRight;
	private JRadioButtonMenuItem rdbtnLegendBottomright;
	private JRadioButtonMenuItem rdbtnLegendBottomleft;
	private JRadioButtonMenuItem rdbtnLegendNone;
	private JMenu mnColor;
	
	public PlotPanel getBasePanel(){
		return BasePanel;
	}
	
	public PlotPaneCollection(PlotColor c,PlotPanelCollectionConfiguration conf) {
		super();
		Col = c;
		Conf = conf;
		setLayout(new BorderLayout(0, 0));

		BasePanel = new PlotPanel(Col);
		BasePanel.setBackground(Color.WHITE);
		BasePanel.setBorder(new EmptyBorder(10, 10, 10, 10));
		add(BasePanel, BorderLayout.CENTER);
		
		BasePanel.setLayout(new BorderLayout(0, Conf.XAxisGap));

		Panel_Channels = new PlotPanel(Col);
		BasePanel.add(Panel_Channels, BorderLayout.CENTER);
		Panel_Channels.setBackground(Color.WHITE);

		JPopupMenu popupMenuChannels = new JPopupMenu();
		popupMenuChannels.addPopupMenuListener(new PopupMenuListener() {
			public void popupMenuCanceled(PopupMenuEvent arg0) {
			}
			public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) {
			}
			public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) {
				switch(Col.getColorMode()){
				case PlotColor.COLOR_BW_BLACK_BACK:
					rdbtnmntmBwBlack.setSelected(true);
					break;
				case PlotColor.COLOR_BW_WHITE_BACK:
					rdbtnmntmColorWhite.setSelected(true);
					break;
				case PlotColor.COLOR_COLOR_BLACK_BACK:
					rdbtnmntmColorBlack.setSelected(true);
					break;
				case PlotColor.COLOR_COLOR_WHITE_BACK:
					rdbtnmntmColorWhite.setSelected(true);
					break;

				}
				switch(getLegendPosition()){
				case PlotPane.LegendPosTOP_LEFT:
					rdbtnLegendTopLeft.setSelected(true);
					break;
				case PlotPane.LegendPosTOP_RIGHT:
					rdbtnLegendTopRight.setSelected(true);
					break;
				case PlotPane.LegendPosBOTTOM_RIGHT:
					rdbtnLegendBottomright.setSelected(true);
					break;
				case PlotPane.LegendPosBOTTOM_LEFT:
					rdbtnLegendBottomleft.setSelected(true);
					break;
				case PlotPane.LegendPosNone:
				default:	
					rdbtnLegendNone.setSelected(true);
					break;
				}
			}
		});
		Panel_Channels.setLayout(new GridLayout(0, 1, 0, 20));
		addPopup(Panel_Channels, popupMenuChannels);
		
		mnColor = new JMenu("Color");
		mnColor.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/rich_text_color.png")));
		popupMenuChannels.add(mnColor);
				rdbtnmntmBwWhite = new JRadioButtonMenuItem("B&W White");
				rdbtnmntmBwWhite.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/wave-b-w.png")));
				mnColor.add(rdbtnmntmBwWhite);
				rdbtnmntmBwWhite.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						Col.setColorMode(PlotColor.COLOR_BW_WHITE_BACK);
						onViewChange();
					}
				});
		buttonGroup.add(rdbtnmntmBwWhite);


		rdbtnmntmBwBlack = new JRadioButtonMenuItem("B&W Black");
		rdbtnmntmBwBlack.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/wave-w-b.png")));
		mnColor.add(rdbtnmntmBwBlack);
		rdbtnmntmBwBlack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Col.setColorMode(PlotColor.COLOR_BW_BLACK_BACK);
				onViewChange();
			}
		});
		buttonGroup.add(rdbtnmntmBwBlack);

		rdbtnmntmColorWhite = new JRadioButtonMenuItem("Color White");
		rdbtnmntmColorWhite.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/wave-rd-w.png")));
		mnColor.add(rdbtnmntmColorWhite);
		rdbtnmntmColorWhite.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Col.setColorMode(PlotColor.COLOR_COLOR_WHITE_BACK);
				onViewChange();
			}
		});
		buttonGroup.add(rdbtnmntmColorWhite);

		rdbtnmntmColorBlack = new JRadioButtonMenuItem("Color Black");
		rdbtnmntmColorBlack.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/wave-rd-b.png")));
		mnColor.add(rdbtnmntmColorBlack);
		rdbtnmntmColorBlack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Col.setColorMode(PlotColor.COLOR_COLOR_BLACK_BACK);
				onViewChange();
			}
		});
		buttonGroup.add(rdbtnmntmColorBlack);
		
	
		
		mnLegend = new JMenu("Legend");
		popupMenuChannels.add(mnLegend);
		
		rdbtnLegendTopLeft = new JRadioButtonMenuItem("Top-Left");
		rdbtnLegendTopLeft.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/LegendTL.png")));
		rdbtnLegendTopLeft.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setLegendPosition(PlotPane.LegendPosTOP_LEFT);
				onViewChange();
			}
		});
		buttonGroup2.add(rdbtnLegendTopLeft);
		mnLegend.add(rdbtnLegendTopLeft);
		
		rdbtnLegendTopRight = new JRadioButtonMenuItem("Top-Right");
		rdbtnLegendTopRight.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/LegendTR.png")));
		rdbtnLegendTopRight.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setLegendPosition(PlotPane.LegendPosTOP_RIGHT);
				onViewChange();
			}
		});
		buttonGroup2.add(rdbtnLegendTopRight);
		mnLegend.add(rdbtnLegendTopRight);
		
		rdbtnLegendBottomright = new JRadioButtonMenuItem("Bottom-Right");
		rdbtnLegendBottomright.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/LegendBR.png")));
		rdbtnLegendBottomright.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setLegendPosition(PlotPane.LegendPosBOTTOM_RIGHT);
				onViewChange();
			}
		});
		buttonGroup2.add(rdbtnLegendBottomright);
		mnLegend.add(rdbtnLegendBottomright);
		
		rdbtnLegendBottomleft = new JRadioButtonMenuItem("Bottom-Left");
		rdbtnLegendBottomleft.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/LegendBL.png")));
		rdbtnLegendBottomleft.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setLegendPosition(PlotPane.LegendPosBOTTOM_LEFT);
				onViewChange();
			}
		});
		buttonGroup2.add(rdbtnLegendBottomleft);
		mnLegend.add(rdbtnLegendBottomleft);
		
		rdbtnLegendNone = new JRadioButtonMenuItem("None");
		//rdbtnLegendNone.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/Legend.png")));
		rdbtnLegendNone.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setLegendPosition(PlotPane.LegendPosNone);
				onViewChange();
			}
		});
		buttonGroup2.add(rdbtnLegendNone);
		mnLegend.add(rdbtnLegendNone);



		PlotPanel panelSouth = new PlotPanel(Col);
		BasePanel.add(panelSouth, BorderLayout.SOUTH);
		panelSouth.setLayout(new BorderLayout(0, 20));

		Panel_XAxis = new PlotAxisXView();
		Panel_TAxis = new PlotAxisTView();
		if(Conf.XAxisType ==PlotPanelCollectionConfiguration.XAxisTypeTime){
			panelSouth.add(Panel_TAxis, BorderLayout.CENTER);
		}
		else if(Conf.XAxisType ==PlotPanelCollectionConfiguration.XAxisTypeX){
			panelSouth.add(Panel_XAxis, BorderLayout.CENTER);
		}
	
		PlotPanel panelMisc = new PlotPanel(Col);
		panelMisc.setPreferredSize(new Dimension(100,50));
		panelSouth.add(panelMisc, BorderLayout.WEST);
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	
	/**
	 * 凡例の表示位置、表示の有無
	 * @param pos
	 */
	public boolean setLegendPosition(int pos){
		if(!channelExistanceCheck()) return false;
		
		for(int i=0;i<Panel_Channels.getComponentCount();i++){
			Component c = Panel_Channels.getComponent(i);
			if(c instanceof PlotPanel){
				Set<Integer> key = Plot.keySet();
				Iterator<Integer> ite = key.iterator();
				while(ite.hasNext()){
					int k = ite.next();
					PlotPanel pp = Plot.get(k).Panel;
					if(pp.equals((PlotPanel)c)){
						// TODO パネルにレジェンドを設定する。
						for(int t=0;t<pp.getComponentCount();t++){
							if( pp.getComponent(t) instanceof PlotPane){
								((PlotPane)pp.getComponent(t)).setLegendPosition(pos);
							}
						}
					}
				}

			}
		}
		return true;
	}
	/**
	 * 
	 * @return
	 */
	public int getLegendPosition(){
		if(!channelExistanceCheck()) return 0;
		
		for(int i=0;i<Panel_Channels.getComponentCount();i++){
			Component c = Panel_Channels.getComponent(i);
			if(c instanceof PlotPanel){
				Set<Integer> key = Plot.keySet();
				Iterator<Integer> ite = key.iterator();
				while(ite.hasNext()){
					int k = ite.next();
					PlotPanel pp = Plot.get(k).Panel;
					if(pp.equals((PlotPanel)c)){
						for(int t=0;t<pp.getComponentCount();t++){
							if( pp.getComponent(t) instanceof PlotPane){
								// 最初に見つかったものを返す。
								return ((PlotPane)pp.getComponent(t)).getLegendPosition();
							}
						}
					}
				}

			}
		}
		return 0;
	}
	
	/**
	 * ビューが変更された場合に再描画する
	 */
	protected void onViewChange(){
		repaintAllComponent();
	}

	/**
	 * 表示チャンネルの有無のチェック
	 * @return
	 */
	private boolean channelExistanceCheck(){
		if(Panel_Channels==null) return false;
		if(AccViewCh.size()<=0) return false;
		return true;
	}
	/**
	 * 複数のプロットを置く場合
	 * @param ch
	 * @param subid
	 */
	private boolean delChannelPanel(int ch, int subid) {
//		if(Panel_Channels==null) return false;
//		if(AccViewCh.size()<=0) return false;
		if(!channelExistanceCheck()) return false;
		
		for(int i=0;i<Panel_Channels.getComponentCount();i++){
			Component c = Panel_Channels.getComponent(i);
			if(c instanceof PlotPanel){
				Set<Integer> key = Plot.keySet();
				Iterator<Integer> ite = key.iterator();
				while(ite.hasNext()){
					int k = ite.next();
					PlotPanel pp = Plot.get(k).Panel;
					if(pp.equals((PlotPanel)c)){
						// パネルからプロットを見つけ出して親をリムーブする
						for(int t=0;t<pp.getComponentCount();t++){
							if( pp.getComponent(t) instanceof PlotAccelaration){
								Panel_TAxis.removeParent((PlotAccelaration)(pp.getComponent(t)));
							}
						}
						Panel_Channels.remove(i);
						///Panel_Channels.validate();
						AccViewCh.remove((Integer)k);
						Plot.remove(k);

						return true;
					}
				}

			}
		}
		return true;
	}
	/**
	 * 複数のグラフ表示の場合
	 * @param ch
	 * @param subid
	 */
	private boolean delChannelPanel(int ch) {
//		if(Panel_Channels==null) return false;
//		if(AccViewCh.size()<=0) return false;
		if(!channelExistanceCheck()) return false;
		
		for(int i=0;i<Panel_Channels.getComponentCount();i++){
			Component c = Panel_Channels.getComponent(i);
			if(c instanceof PlotPanel){
				Set<Integer> key = Plot.keySet();
				Iterator<Integer> ite = key.iterator();
				while(ite.hasNext()){
					int k = ite.next();
					PlotPanel pp = Plot.get(k).Panel;
					if(pp.equals((PlotPanel)c)){
						Panel_Channels.remove(i);
						Panel_Channels.validate();
						AccViewCh.remove((Integer)k);
						Plot.remove(k);
						return true;
					}
				}
			}
		}
		return true;
	}


	private boolean isChannelExist(int ch,int sub){
		ch = ch + sub * 0x10000;
		return Parent.isChannelExist(ch);
	}
	private boolean isChannelExist(int ch){
		return Parent.isChannelExist(ch);
	}

	/**
	 * CH設定が更新されたとき、コンポーネントを追加削除する
	 */
	/**
	 * マルチプロット型
	 * @param sel   選択されているＣＨのリスト
	 * @param sel2　 すべてのＣＨのりすと
	 * @param subid2　選択されているサブＩＤ
	 * @param subid　　すべてのサブＩＤ
	 */
	public void onUpdateChannelMultiPlot(Integer[] sel,Integer[] sel2,Integer[] subid2,Integer[] subid){
		//Integer[] subid = getSubId(true); // すべてのオプションを得る
		// 選択されていないｃｈのすべてを削除する
		//Integer[] sel = getChannelList(false);
		if(sel!=null && subid !=null){
			for(int i=0;i<sel.length;i++){
				int id1 = sel[i];
				for(int j=0;j<subid.length;j++){
					int id2 = subid[j];
					if(isChannelExist(id1,id2)){
						//System.out.println("delChannelPanel " + String.format(" %04x ", id1) + String.format("%04x", id2));
						if(!delChannelPanel(id1, id2)){
							System.out.println("Fail to delChannelPanel (bug)" + id1 + id2);
						}
					}
				}
			}
		}
		// 選択されているｃｈのすべてを追加する
		//Integer[] subid2 = getSubId(false); // 選択されているオプションを得る
		//Integer[] sel2 = getChannelList(true);
		if(sel2!=null && subid2!=null){
			for(int i=0;i<sel2.length;i++){
				int id1 = sel2[i];
				for(int j=0;j<subid2.length;j++){
					int id2 = subid2[j];
					if(!isChannelExist(id1,id2)){
						//System.out.println("addChannelPanel " + String.format(" %04x ", id1) + String.format("%04x", id2));
						//addChannelPanel(id1,id2);
						Parent.onSetData(id1, id2);
					}
				}
			}
		}
		revalidate();	/// これで、再描画してくれる。
		// delay してオートＡＭＰを押す
		DelayThread d = new DelayThread();
		d.Parent=this;
		d.Millisec=100;
		d.start();
	}
	/**
	 * マルチチャンネル型
	 * @param sel
	 * @param sel2
	 * @param subid2
	 * @param subid
	 */
	public void onUpdateChannelMultiChannel(Integer[] sel,Integer[] sel2,Integer[] subid2,Integer[] subid){
		//Integer[] subid = getSubId(true); // すべてのオプションを得る
		// 選択されていないｃｈのすべてを削除する
		//Integer[] sel = getChannelList(false);
		if(sel!=null && subid !=null){
			for(int i=0;i<sel.length;i++){
				int id1 = sel[i];
				if(isChannelExist(id1)){
					//System.out.println("delChannelPanel " + String.format(" %04x ", id1) + String.format("%04x", id2));
					if(!delChannelPanel(id1)){
						System.out.println("Fail to delChannelPanel (bug)" + id1);
					}
				}
			}
		}
		// 選択されているｃｈのすべてを追加する
		//Integer[] sel2 = getChannelList(true);
		if(sel2!=null){
			LinkedList<Integer> chtable = new LinkedList<Integer>();
			for(int i=0;i<sel2.length;i++){
				int id1 = sel2[i];
				if(!isChannelExist(id1)){
					chtable.add(id1);
					//System.out.println("addChannelPanel " + String.format(" %04x ", id1) + String.format("%04x", id2));
					//addChannelPanel(id1);
				}
			}
			Integer[] a = chtable.toArray(new Integer[0]);
			//addChannelPanel(a);
			Parent.onSetData(a);
		}
		revalidate();	/// これで、再描画してくれる。
		// delay してオートＡＭＰを押す
		DelayThread d = new DelayThread();
		d.Parent = this;
		d.Millisec = 100;
		d.start();
	}


	/**
	 * データパネルを追加する
	 * @param id　int channle ID
	 * @param name String 名前
	 * @param data　double[] データ
	 * @param smp　int サンプリングレート
	 */
	public void addChannelPanel(int id, String name, double[] data, int smp,winDataBase db) {
		addChannelPanel(Panel_Channels,id,name,data,smp,db);
	}
	private void addChannelPanel(JPanel panel_channels,int id, String name, double[] data, int smp,winDataBase db) {
		if(AccViewCh.contains(id)) return; // 既に 登録されている
		PlotPanel panel_singleChannel1 = new PlotPanel(Col);			// チャンネルを乗せるパネル
		//Panel_Channels.add(panel_singleChannel1);
		panel_singleChannel1.setLayout(new BorderLayout(0, 0));

		PlotAxisYView panel_Yaxis = new PlotAxisYView();
		panel_Yaxis.setPreferredSize(new Dimension(100,10));
		panel_Yaxis.setLayout(null);
		panel_singleChannel1.add(panel_Yaxis, BorderLayout.WEST);
		panel_Yaxis.setAxisDivide(2,true);


		PlotAccelaration panel_graph = new PlotAccelaration();
		panel_graph.setPreferredSize(new Dimension(100,10));
		panel_graph.setPlotColor(Col);
		panel_graph.setSampleRate(smp,false);
		//wintime tm = new wintime(Data.getDispStartTime());
		wintime tm = new wintime(db.getDispStartTime());
		panel_graph.setDataStartTime(tm,false);
		panel_graph.setViewTimeStart(0,false);
		panel_graph.setViewTimeSpan(data.length,false);
		panel_graph.setData(id & 0xffff, (id >> 16)&0xffff,name, data);

		panel_singleChannel1.add(panel_graph, BorderLayout.CENTER);
		panel_graph.setYaxis(panel_Yaxis,Col);	// グラフエリアにＹ軸表示エリアを関連づける

		//		panel_Yaxis.title1 = String.format("%04x", id&0xffff);
		//		panel_Yaxis.title2 = name;

		// 管理用
		AccViewCh.addLast(id);

		Plot.put(id, new PlotTable(panel_graph,panel_singleChannel1));

		panel_channels.add(panel_singleChannel1);
		/**
		 * Ｙ軸をセットする
		 */
		if(Panel_TAxis!=null){
			panel_graph.setTaxis(Panel_TAxis, Col);
		}

	}


	/**
	 * すべてを再描画する
	 */
	public void repaintAllComponent() {
		this.repaint();
		BasePanel.repaint();
		Iterator<Integer> ite = AccViewCh.iterator();
		while(ite.hasNext()){
			int ch = ite.next();
			PlotPanel pp=Plot.get(ch).Panel;
			pp.repaint();
			//PlotAccelaration ac=Plot.get(ch).AcsView;
			PlotPane ac=Plot.get(ch).AcsView;
			ac.repaintAll();
		}
	}

	/* (non-Javadoc)
	 * @see ikelib.DelayThreadInterface#onDelayCallback(int)
	 */
	@Override
	public void onDelayCallback(int id) {
		Parent.onInitView(0);

	}


	/**
	 * @param panel_Channels2
	 * @param xid
	 * @param ids
	 * @param names
	 * @param datas
	 * @param i
	 * @param xdat
	 */
	public void addChannelPanel(int xid, Integer[] ids, LinkedList<String> name, LinkedList<double[]> datas, int smp, double[] xdata) {
		addChannelPanel(Panel_Channels,xid,ids,name , datas, smp, xdata );
	}
	private void addChannelPanel(JPanel panel_channels,int xid, Integer[] ids, LinkedList<String> name, LinkedList<double[]> datas, int smp, double[] xdata) {
		
		if(AccViewCh.contains(xid)) return; // 既に 登録されている

		PlotPanel panel_singleChannel1 = new PlotPanel(Col);			// チャンネルを乗せるパネル
		//Panel_Channels.add(panel_singleChannel1);
		panel_singleChannel1.setLayout(new BorderLayout(0, 0));

		PlotAxisYView panel_Yaxis = new PlotAxisYView();
		panel_Yaxis.setPreferredSize(new Dimension(100,10));
		panel_Yaxis.setLayout(null);
		panel_singleChannel1.add(panel_Yaxis, BorderLayout.WEST);

		PlotTemperature panel_graph = new PlotTemperature();
		panel_graph.setPreferredSize(new Dimension(100,10));
		panel_graph.setPlotColor(Col);
		
		for(int i=0;i<ids.length;i++){
			int id = ids[i];
			double[] data = datas.get(i);
			panel_graph.setData(id & 0xffff, (id >> 16)&0xffff,name.get(i), data,xid,xdata);
		}

		panel_singleChannel1.add(panel_graph, BorderLayout.CENTER);
		panel_graph.setYaxis(panel_Yaxis,Col);	// グラフエリアにＹ軸表示エリアを関連づける

		// 管理用
		AccViewCh.addLast(xid);

		Plot.put(xid, new PlotTable(panel_graph,panel_singleChannel1));

		panel_channels.add(panel_singleChannel1);
		/**
		 * 
		 * 軸をセットする
		 */
		if(Panel_XAxis!=null){
			Panel_XAxis.clearParent();
			panel_graph.setXaxis(Panel_XAxis, Col);
		}

	}

}
